/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CrearCanal.java
 *
 * Created on 26/01/2012, 02:43:59 PM
 */
package GUI;

import Control.CanalJpaController;
import Control.exceptions.PreexistingEntityException;
import Entidad.Canal;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author pepe
 */
public class CrearCanal extends javax.swing.JPanel {

    /** Creates new form CrearCanal */
    public CrearCanal() {
        initComponents();
        controlCanal = new CanalJpaController();
        buttonGroup1.add(internacionalRB);
        buttonGroup1.add(nacionalRB);
        
    }
    
    Canal c;
    CanalJpaController controlCanal;

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jLabel1 = new javax.swing.JLabel();
        nacionalRB = new javax.swing.JRadioButton();
        internacionalRB = new javax.swing.JRadioButton();
        nombreTF = new javax.swing.JTextField();
        crearJB = new javax.swing.JButton();
        cancelarJB = new javax.swing.JButton();
        statussJL = new javax.swing.JLabel();

        jLabel1.setText("Nombre");

        nacionalRB.setText("Nacional");

        internacionalRB.setText("Internacional");

        crearJB.setText("Crear");
        crearJB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                crearJBActionPerformed(evt);
            }
        });

        cancelarJB.setText("Cancelar");
        cancelarJB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelarJBActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(85, 85, 85)
                        .addComponent(nacionalRB)
                        .addGap(18, 18, 18)
                        .addComponent(internacionalRB))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(nombreTF, javax.swing.GroupLayout.DEFAULT_SIZE, 333, Short.MAX_VALUE)))
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(71, 71, 71)
                .addComponent(crearJB)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 100, Short.MAX_VALUE)
                .addComponent(cancelarJB)
                .addGap(95, 95, 95))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(statussJL, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(nombreTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(nacionalRB)
                    .addComponent(internacionalRB))
                .addGap(12, 12, 12)
                .addComponent(statussJL, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cancelarJB)
                    .addComponent(crearJB))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

private void crearJBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_crearJBActionPerformed
    c = new Canal();
    
    if(nombreTF.getText().equals("") || (!nacionalRB.isSelected() && !internacionalRB.isSelected())){
        statussJL.setText("No pueden haber campos vacios o sin seleccionar");
    }else{
        c.setNombre(nombreTF.getText());
        c.setNacional(nacionalRB.isSelected());

        try {
            controlCanal.create(c);
            statussJL.setText("Canal creado exitosamente");
        } catch (PreexistingEntityException ex) {
            Logger.getLogger(CrearCanal.class.getName()).log(Level.SEVERE, null, ex);
            statussJL.setText("Ya hay un canal con el mismo nombre");
        } catch (Exception ex) {
            Logger.getLogger(CrearCanal.class.getName()).log(Level.SEVERE, null, ex);
            statussJL.setText("No se ha podido crear el canal");
        }
    }
    
    
}//GEN-LAST:event_crearJBActionPerformed

private void cancelarJBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelarJBActionPerformed
    nombreTF.setText("");
    statussJL.setText("");
    nacionalRB.setSelected(false);
    internacionalRB.setSelected(false);
}//GEN-LAST:event_cancelarJBActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JButton cancelarJB;
    private javax.swing.JButton crearJB;
    private javax.swing.JRadioButton internacionalRB;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JRadioButton nacionalRB;
    private javax.swing.JTextField nombreTF;
    private javax.swing.JLabel statussJL;
    // End of variables declaration//GEN-END:variables
}
